package com.lin.operatorlogrecord.controller;

import com.lin.operatorlogrecord.entity.annotation.OperatorLogAnnotation;
import com.lin.operatorlogrecord.entity.enums.LogTypeEnum;
import com.lin.operatorlogrecord.entity.model.Student;
import com.lin.operatorlogrecord.mapper.StudentMapper;
import com.lin.operatorlogrecord.strategy.AddOperatorLogStrategy;
import com.lin.operatorlogrecord.strategy.DeleteOperatorLogStrategy;
import com.lin.operatorlogrecord.strategy.UpdateOperatorLogStrategy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentMapper studentMapper;


    @PostMapping
    @OperatorLogAnnotation(requestObjSpel = "#student", iOperatorLogStrategy = AddOperatorLogStrategy.class,
            tableEntity = Student.class, logType = LogTypeEnum.USER_OPERATOR)
    public String create(@RequestBody Student student) {
        studentMapper.insert(student);
        return student.getObjId();
    }

    @DeleteMapping("/{id}")
    @OperatorLogAnnotation(operatorObjIdSpel = "#id", iOperatorLogStrategy = DeleteOperatorLogStrategy.class,
            tableEntity = Student.class, logType = LogTypeEnum.USER_OPERATOR)
    public void delete(@PathVariable("id") String id) {
        studentMapper.deleteById(id);
    }

    @PutMapping
    @OperatorLogAnnotation(requestObjSpel = "#student", operatorObjIdSpel = "#student.getObjId()", iOperatorLogStrategy = UpdateOperatorLogStrategy.class,
            tableEntity = Student.class, logType = LogTypeEnum.USER_OPERATOR)
    public void update(@RequestBody Student student) {
        studentMapper.updateById(student);
    }
}
